import pygame


class ActionByMulti:
    def __init__(self, path_expression: str, image_count: int, is_loop: bool):
        """
        初始化
        :param path_expression: 图片路径
        :param image_count: 图片数量
        :param is_loop: 是否循环
        """
        self.image_index = 0
        self.action_images = []
        self.image_count = image_count
        self.is_loop = is_loop

        for i in range(0, image_count):
            img_path = str.format(path_expression % (i + 1))
            self.action_images.append(pygame.image.load(img_path))

    def get_image(self) -> pygame.Surface:
        """
        获取当前显示图片
        :return:
        """
        current_img = self.action_images[self.image_index]
        if self.image_index + 1 >= self.image_count:
            if self.is_loop:
                self.image_index = 0
        else:
            self.image_index += 1
        return current_img

    def is_end(self) -> bool:
        """
        图片是否绘制结束
        :return: True
        """
        if self.is_loop:
            return False
        else:
            if self.image_index >= self.image_count - 1:
                return True
            else:
                return False


class WelcomeSprite(pygame.sprite.Sprite):
    """
       初始化多张图片组成的精灵
        :param x: 精灵坐标x
        :param y: 精灵坐标y
        :param path: 图片地址
        :param num: 图片数量
    """

    def __init__(self, x: int, y: int, path: str, num: int):
        super(WelcomeSprite, self).__init__()
        self.action = ActionByMulti(path, num, True)
        self.pos_x = x
        self.pos_y = y

    def draw(self, surface):
        """
        精灵绘制
        :param surface: 绘制surface
        :return:
        """
        current_img = self.action.get_image()
        surface.blit(current_img, (self.pos_x, self.pos_y))

    def run(self, down_flag, key_list):
        """
        精灵运动,主动运动
        :return:
        """
        if self.pos_x < 1050:
            self.pos_x += 1
